package cn.sinata.wanglin.publish.activity;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.xilada.xldutils.activitys.DialogActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.DensityUtil;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.R;
/**
 * 发布主页
 */
public class PubhomeActivity extends DialogActivity {
    @BindView(R.id.tv_item1)
    TextView tv_item1;//
    @BindView(R.id.tv_item2)
    TextView tv_item2;//
    @BindView(R.id.tv_item3)
    TextView tv_item3;//
    @BindView(R.id.tv_item4)
    TextView tv_item4;//
    @BindView(R.id.iv_pubclose)
    ImageView iv_pubclose;//
    @Override
    protected int setContentLayout() {
        return R.layout.activity_pubhome;
    }

    @Override
    protected void initView() {
        setCancelOnTouchOutside(true);
        String str1= tv_item1.getText().toString();
        tv_item1.setText(changeTextSize(str1));

        String str2= tv_item2.getText().toString();
        tv_item2.setText(changeTextSize(str2));

        String str3= tv_item3.getText().toString();
        tv_item3.setText(changeTextSize(str3));

        String str4= tv_item4.getText().toString();
        tv_item4.setText(changeTextSize(str4));

        final Animation mAnimation2=  setAnima(tv_item2);
        final Animation mAnimation3=  setAnima(tv_item3);
        final Animation mAnimation4=  setAnima(tv_item4);
        final Animation mAnimation5=  setAnima(iv_pubclose);
        mAnimation2.setDuration(300);
        mAnimation3.setDuration(500);
        mAnimation4.setDuration(700);
        mAnimation5.setDuration(900);
        tv_item2.startAnimation(mAnimation2);
        tv_item3.startAnimation(mAnimation3);
        tv_item4.startAnimation(mAnimation4);
        iv_pubclose.startAnimation(mAnimation5);

    }

    @Override
    protected void setLayout() {
        super.setLayout();
        getWindow().setLayout((int) (mScreenWidth*0.8f), ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.CENTER);
    }

    public SpannableStringBuilder changeTextSize(String str){
        SpannableStringBuilder builderTemp= new SpannableStringBuilder(str);
        builderTemp.setSpan(new AbsoluteSizeSpan(16,true),0,4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return builderTemp;
    }
    public Animation setAnima(final View view){
        final float toXDelta= view.getX();
        final float fromXDelta= DensityUtil.getDeviceWidth(this);
        final float fromYDelta =view.getY();
        final float toYDelta =view.getY();

        Animation translateAnimation = new TranslateAnimation(fromXDelta,toXDelta,fromYDelta,toYDelta);
        //设置动画时间
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //摆动动画
                Animation translateAnimation2 = new TranslateAnimation(toXDelta,toXDelta+30,fromYDelta,toYDelta);
                translateAnimation2.setDuration(100);
                translateAnimation2.setFillAfter(true);
                view.startAnimation(translateAnimation2);
                translateAnimation2.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        Animation translateAnimation3 = new TranslateAnimation(toXDelta+30,toXDelta,fromYDelta,toYDelta);
                        translateAnimation3.setDuration(100);
                        translateAnimation3.setFillAfter(true);
                        view.startAnimation(translateAnimation3);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        return translateAnimation;
    }

    @OnClick({R.id.tv_item1,R.id.tv_item2,R.id.tv_item3,R.id.tv_item4, R.id.iv_pubclose,R.id.dialog_root})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_item1:
                //描述:小区吐槽
                ActivityUtil.create(this).go(PubsayingActivity.class)
                        .startForResult(1);
                break;
            case R.id.tv_item2:
                //描述:同城八卦
                ActivityUtil.create(this).go(PubsamecityActivity.class)
                        .startForResult(2);
                break;
            case R.id.tv_item3:
                //描述:发圈子

                ActivityUtil.create(this).go(PubcircleActivity.class)
                        .startForResult(3);
                break;
            case R.id.tv_item4:
                //描述:小区活动
                ActivityUtil.create(this).go(PubactivityActivity.class)
                        .startForResult(4);
                break;
            case R.id.iv_pubclose:
                //描述:关闭
                 finish();
                break;
            case R.id.dialog_root:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
          switch (requestCode){
              case 1:
                  setResult(3);
                  finish();
                  break;
              case 2:
                  setResult(4);
                  finish();
                  break;
              case 3:
                  finish();
                  break;
              case 4:
                  setResult(5);
                  finish();
                  break;
          }

        }
    }
}
